package cn.manyufun.myf.ui.common.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.ComicsSummary;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.common.irecyclerview.universaladapter.ViewHolderHelper;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import cn.manyufun.myf.ui.comics.activity.ComicsDetailActivity;

/**
 * Created by Xiao on 2018-01-31.
 */

public class CommonListAdapter extends CommonRecycleViewAdapter<ComicsSummary> {
    private static final String TAG = "CommonListAdapter";

    public CommonListAdapter(Context context, int layoutId, List<ComicsSummary> mDatas) {
        super(context, layoutId, mDatas);
    }

    @Override
    public void convert(ViewHolderHelper holder, final ComicsSummary followSummary) {
        holder.setText(R.id.tv_comics_title, followSummary.getTitle());
        holder.setText(R.id.tv_score, followSummary.getScore() + "");
        holder.setText(R.id.tv_author_name, followSummary.getAuthor());
        holder.setText(R.id.tv_follow_num, followSummary.getFollowNum() + "");
        holder.setText(R.id.tv_comment_num, followSummary.getCommentNum() + "");
        holder.setText(R.id.tv_view_num, followSummary.getViewNum() + "");
        holder.setImageRoundCornerUrl(R.id.iv_comics_photo, followSummary.getCoverImages().get(0), DisplayUtil.dip2px(8f));
        if (followSummary.isFollowed())
            holder.setText(R.id.tv_follow_str, "已收藏");
        else
            holder.setText(R.id.tv_follow_str, "未收藏");

        holder.setRating(R.id.rb_score, followSummary.getScore(), 5);

        holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putLong(ComicsDetailActivity.FILTER, followSummary.getComicId());
                ComicsDetailActivity.startAction(mContext, bundle);
            }
        });
        LinearLayout mLLType = holder.getView(R.id.ll_type);
        mLLType.removeAllViews();
        List<String> type = followSummary.getComicType();
        for (String t : type) {
            TextView tv = (TextView) ((Activity) mContext).getLayoutInflater().inflate(R.layout.type_text, null);
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            param.setMarginEnd(DisplayUtil.dip2px(5f));
            tv.setLayoutParams(param);
            tv.setText(t);
            mLLType.addView(tv);
        }
    }
}
